package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.HoOccupancyInfo;
import com.ruoyi.system.service.IHoOccupancyInfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.HoChangeApplication;
import com.ruoyi.system.service.IHoChangeApplicationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 换宿申请Controller
 *
 * @author ruoyi
 * @date 2025-01-10
 */
@RestController
@RequestMapping("/system/changeApplication")
public class HoChangeApplicationController extends BaseController {
    @Autowired
    private IHoChangeApplicationService hoChangeApplicationService;

    @Autowired
    private IHoOccupancyInfoService hoOccupancyInfoService;

    /**
     * 查询换宿申请列表
     */
    @PreAuthorize("@ss.hasPermi('system:changeApplication:list')")
    @GetMapping("/list")
    public TableDataInfo list(HoChangeApplication hoChangeApplication) {
        startPage();
        if (null != hoChangeApplication.getStudentId() && hoChangeApplication.getStudentId() == 0) {
            hoChangeApplication.setStudentId(null);
        } else {
            hoChangeApplication.setStudentId(getUserId());
        }
        List<HoChangeApplication> list = hoChangeApplicationService.selectHoChangeApplicationList(hoChangeApplication);
        return getDataTable(list);
    }

    /**
     * 导出换宿申请列表
     */
    @PreAuthorize("@ss.hasPermi('system:changeApplication:export')")
    @Log(title = "换宿申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HoChangeApplication hoChangeApplication) {
        if (hoChangeApplication.getStudentId() == 0) {
            hoChangeApplication.setStudentId(null);
        } else {
            hoChangeApplication.setStudentId(getUserId());
        }
        List<HoChangeApplication> list = hoChangeApplicationService.selectHoChangeApplicationList(hoChangeApplication);
        ExcelUtil<HoChangeApplication> util = new ExcelUtil<HoChangeApplication>(HoChangeApplication.class);
        util.exportExcel(response, list, "换宿申请数据");
    }

    /**
     * 获取换宿申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:changeApplication:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(hoChangeApplicationService.selectHoChangeApplicationById(id));
    }

    /**
     * 新增换宿申请
     */
    @PreAuthorize("@ss.hasPermi('system:changeApplication:add')")
    @Log(title = "换宿申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HoChangeApplication hoChangeApplication) {
        HoOccupancyInfo param = new HoOccupancyInfo();
        param.setStudentNum(getLoginUser().getUser().getUserName());
        param.setStatus("1");
        List<HoOccupancyInfo> hoOccupancyInfos = hoOccupancyInfoService.selectHoOccupancyInfoList(param);
        if (CollectionUtils.isEmpty(hoOccupancyInfos)) {
            throw new RuntimeException(getLoginUser().getUser().getNickName() + "|" + getLoginUser().getUser().getUserName() + " 当前状态不是在宿状态，不能进行换宿申请!");
        }
        hoChangeApplication.setStudentId(getUserId());
        hoChangeApplication.setName(getLoginUser().getUser().getNickName());
        hoChangeApplication.setStudentNum(getLoginUser().getUser().getUserName());
        hoChangeApplication.setApplicationTime(DateUtils.getNowDate());
        hoChangeApplication.setReviewStatus("2");
        hoChangeApplication.setCreateUserId(getUserId());
        return toAjax(hoChangeApplicationService.insertHoChangeApplication(hoChangeApplication));
    }

    /**
     * 修改换宿申请
     */
    @PreAuthorize("@ss.hasPermi('system:changeApplication:edit')")
    @Log(title = "换宿申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HoChangeApplication hoChangeApplication) {
        HoChangeApplication old = hoChangeApplicationService.selectHoChangeApplicationById(hoChangeApplication.getId());
        if (!"2".equals(old.getReviewStatus())) {
            throw new RuntimeException("不是未审核状态的不能进行操作！");
        }
        if (null == old.getDormitoryId()) {
            throw new RuntimeException("换宿宿舍不能为空！");
        }
        if ("3".equals(hoChangeApplication.getReviewStatus())) {
            // 撤回
        } else if ("1".equals(hoChangeApplication.getReviewStatus())) {
            // 审核通过
            HoOccupancyInfo param = new HoOccupancyInfo();
            param.setStudentNum(old.getStudentNum());
            param.setStatus("1");
            List<HoOccupancyInfo> hoOccupancyInfos = hoOccupancyInfoService.selectHoOccupancyInfoList(param);
            if (CollectionUtils.isEmpty(hoOccupancyInfos)) {
                throw new RuntimeException(old.getName() + "|" + old.getStudentNum() + " 当前状态不是在宿状态，不能换宿房间!");
            }
            HoOccupancyInfo hoOccupancyInfo = hoOccupancyInfos.get(0);
            hoOccupancyInfoService.changeApplication(hoOccupancyInfo, old.getDormitoryId(), getUserId(), old.getApplicationInfo(), old.getRequestedTime());
        } else if ("0".equals(hoChangeApplication.getReviewStatus())) {
            // 审核拒绝
        }
        return toAjax(hoChangeApplicationService.updateHoChangeApplication(hoChangeApplication));
    }

    /**
     * 删除换宿申请
     */
    @PreAuthorize("@ss.hasPermi('system:changeApplication:remove')")
    @Log(title = "换宿申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(hoChangeApplicationService.deleteHoChangeApplicationByIds(ids));
    }
}
